﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class SettlementService : MonoBehaviour {

	


	//---------------------------------------------------------------------------------------------
	// Properties
	
	public float globalFoodPrice;
	public float globalWaterPrice;
	public float globalHoochPrice;
	public float globalPartsPrice;
	
	public float localFoodVariation;
	public float localWaterVariation;
	public float localHoochVariation;
	public float localPartsVariation;
	
	public float localFoodPrice;
	public float localWaterPrice;
	public float localHoochPrice;
	public float localPartsPrice;
	
	//---------------------------------------------------------------------------------------------
	// Members 

	public GameObject CurrentSettlement;
	public Map_Settlements currentSettlementScript;
	public List<GameObject> Settlements;
	public List<GameObject> RandomEvents;
	public PlayerStorage Main;
	public Text Cash;
	public Text PanelTitle;

	
	
	//---------------------------------------------------------------------------------------------
	// Init
	void OnEnable()
	{
		Main.LoadValues();
		StartRandomEvent();
		
		CurrentSettlement = Settlements[PlayerPrefs.GetInt("ActiveSettlement")];
		currentSettlementScript = CurrentSettlement.GetComponent<Map_Settlements>();
		
	}
	
	
	//---------------------------------------------------------------------------------------------
	// Update
	void Update()
	{
		
		Cash.text = "$" + Main.cash.ToString();
		
		if (Main.Sound == 1)
		{
			AudioListener.pause = true;
		}
		else
		{
			AudioListener.pause = false;
		}
		
		
		
	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions  
	
	public void StartRandomEvent()
	{
		int diceRoll = Random.Range (0,100);
		if (diceRoll > 50)
		{
			int randomEventNo = Random.Range (0, RandomEvents.Count);
			
			RandomEvents[randomEventNo].SetActive(true);
		}
		
		
	}
	
	//---------------------------------------------------------------------------------------------
	public void RetrieveLocalPrices()
	{
		localFoodVariation = currentSettlementScript.foodVariation;
		localWaterVariation = currentSettlementScript.waterVariation;
		localHoochVariation = currentSettlementScript.hoochVariation;
		localPartsVariation = currentSettlementScript.partsPrice;
	
		float foodVariation = Mathf.Floor(Random.Range (localFoodVariation, -localFoodVariation));
		float waterVariaton = Mathf.Floor(Random.Range (localWaterVariation, -localWaterVariation));
		float hoochVariation = Mathf.Floor(Random.Range (localHoochVariation, -localHoochVariation));
		float partsVariation = Mathf.Floor(Random.Range (localPartsVariation, -localPartsVariation));
	
		localFoodPrice = currentSettlementScript.foodPrice + foodVariation;
		localWaterPrice = currentSettlementScript.waterPrice + waterVariaton;
		localHoochPrice = currentSettlementScript.hoochPrice + hoochVariation;
		localPartsPrice = currentSettlementScript.partsPrice+ partsVariation;
	
	}
	
	//---------------------------------------------------------------------------------------------


	
}
